package com.zju.cst.classification.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

import com.zju.cst.spider.Parse;
import com.zju.cst.spider.domain.News;
import com.zju.cst.spider.mapper.NewsMapper;
import com.zju.cst.spider.utils.DBUtils;
import com.zju.cst.spider.utils.URLUtils;

public class Split {
	
	private final static Logger LOG = Logger.getLogger(Split.class);
	
	private static SplitMethod split = new AnsJSplit();
	
	public static Map<String,Integer> getWordMapFromList(List<String> list){
		LOG.debug("词频统计!");
		Map<String,Integer> wordMap = new HashMap<String,Integer>();
		for (String word : list) {
			if(wordMap.containsKey(word)){
				wordMap.put(word, wordMap.get(word)+1);
			}else {
				wordMap.put(word, 1);
			}
		}
		return wordMap;
	}
	
	public static Map<String,Integer> getWordMapFromText(String text){
		return getWordMapFromList(split.getSplit(text));
	}
	
	public static Map<String,Integer> getWordMapFromUrl(String url){
		String html = URLUtils.getContent(url);
		Document document = Jsoup.parse(html);
		String text = Parse.getText(document);
		return getWordMapFromList(split.getSplit(text));
	}
	
	public static Map<String,Integer> getWordMapFromClassify(String Classify){
		SqlSession session = DBUtils.getSession();
		NewsMapper mapper = session.getMapper(NewsMapper.class);
		List<News> list = mapper.selectByClassify(Classify);
		StringBuilder sb = new StringBuilder();
		for (News news : list) {
			sb.append(news.getText());
		}
		return getWordMapFromText(sb.toString());
	}
		
	@Test
	public void test(){
		String str = "说";
		System.out.println(str.matches("(\\d*)||([\u2E80-\uFE4F])||(\\s*)"));
	}
}
